﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    Ingredient ingredient;
    ingredientDAO dao;
    DataSet dataUpdate;
    protected void Page_Load(object sender, EventArgs e)
    {
        
        ingredient = new Ingredient();
        dao = new ingredientDAO();
        dataUpdate = new DataSet();
        dataUpdate = (DataSet)Session["INGRE"];

        if (!IsPostBack)
        {
            load_cbbCategory();
            load_cbbAnimal();
            LabelID.Text = dataUpdate.Tables[0].Rows[0][0].ToString();
            //   LabelName.Text = dataUpdate.Tables[0].Rows[0][2].ToString();
            TextBox1.Text = dataUpdate.Tables[0].Rows[0][2].ToString();
            //   LabelCate.Text = dataUpdate.Tables[0].Rows[0][12].ToString();
            cbbCategory.Items.FindByText(dataUpdate.Tables[0].Rows[0][12].ToString()).Selected = true;
            //    LabelPrice.Text = dataUpdate.Tables[0].Rows[0][4].ToString();
            TextBox2.Text = dataUpdate.Tables[0].Rows[0][4].ToString();
            //    LabelDry.Text = dataUpdate.Tables[0].Rows[0][5].ToString();
            TextBox3.Text = dataUpdate.Tables[0].Rows[0][5].ToString();
            //   LabelCode.Text = dataUpdate.Tables[0].Rows[0][7].ToString();
            TextBox4.Text = dataUpdate.Tables[0].Rows[0][7].ToString();
            if (dataUpdate.Tables[0].Rows[0][8].Equals(true))
            {
                //          LabelStock.Text = "Still in stock";
                CheckBox1.Checked = true;
            }
            else //LabelStock.Text = "Not in stock";
                CheckBox1.Checked = false;
            //   LabelAnimal.Text = dataUpdate.Tables[0].Rows[0][10].ToString();
            cbbAnimal.Items.FindByText(dataUpdate.Tables[0].Rows[0][10].ToString()).Selected = true;
        }
    
        

        load_data();

        

    }
    public void load_data()
    {
        DataSet ds1 = new DataSet();

        String ingreID = LabelID.Text;
        ds1 = dao.getComposition(ingreID);
        GridView1.DataSource = ds1.Tables[0];
        GridView1.DataBind();
    }
    public void load_cbbCategory()
    {
        DataSet ds2 = new DataSet();
        ds2 = dao.getcbbCategory();
        cbbCategory.DataSource = ds2.Tables[0];
        cbbCategory.DataTextField = "Category";
        cbbCategory.DataValueField = "CategoryID";
        cbbCategory.DataBind();
    }
    public void load_cbbAnimal()
    {
        DataSet ds3 = new DataSet();
        ds3 = dao.getcbbAnimal();
        cbbAnimal.DataSource = ds3.Tables[0];
        cbbAnimal.DataTextField = "Animal";
        cbbAnimal.DataValueField = "AnimalID";
        cbbAnimal.DataBind();
    }
    public void update()
    {
        DataSet ds = new DataSet();
        ds = (DataSet)Session["USER"];
        String userID = ds.Tables[0].Rows[0]["UserID"].ToString();
        ingredient.IngredientID1 = dataUpdate.Tables[0].Rows[0][0].ToString().Trim();
        ingredient.Name1 = TextBox1.Text;
        ingredient.Price1 = Double.Parse(TextBox2.Text);
        ingredient.Dry1 = float.Parse(TextBox3.Text);
        ingredient.CategoryID1 = cbbCategory.SelectedValue.Trim();
        ingredient.Code1 = TextBox4.Text;
        ingredient.AnimalID1 = cbbAnimal.SelectedValue.Trim();
        if (CheckBox1.Checked)
        {
            ingredient.InStock1 = true;
        }
        else ingredient.InStock1 = false;

        dao.update(ingredient);

        
        Response.Redirect("home.aspx");
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        update();
    }
}